package com.orm.handler;


import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


// 所有类型处理器的基类， 处理了一下 null 值
public abstract class BaseTypeHandler<T> implements TypeHandler<T>{
	
	@Override
	public void setParameter(PreparedStatement ps, int i, T parameter, JDBCType jdbcType) throws SQLException {
		if(parameter == null){
			ps.setObject(i,null);   // 只要参数为null ，不管什么类型都用object 直接填null 拉倒
		}else{
			setNotNullParameter(ps,i,parameter,jdbcType);
		}
	}
	
	@Override
	public T getResult(ResultSet rs, String columnName) throws SQLException {
		T result;
		result = getNullableResult(rs, columnName);
		
		if(rs.wasNull()){
			return null;      // 直接用result 判断不行么?
		}else{
			return result;
		}
	}
	
	@Override
	public T getResult(ResultSet rs, int columnIndex) throws SQLException {
		T result;
		result = getNullableResult(rs, columnIndex);
		
		if(rs.wasNull()){
			return null;
		}else{
			return result;
		}
	}
	
	
	public abstract void setNotNullParameter(PreparedStatement ps, int i, T parameter, JDBCType jdbcType) throws SQLException;
	
	public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;
	
	public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;
}
